package com.shop.front.controller.user;


import com.shop.domain.user.UserBankCard;
import com.shop.front.request.user.UserBankCardReq;
import com.shop.front.service.user.UserBankCardService;
import com.shop.front.shiro.UserTokenUtil;
import com.shop.web.context.WebContext;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

@Api(description = "用户银行卡信息相关", position = -1)
@RestController
@RequestMapping(value = "/userBankCard", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class UserBankCardController {

    @Autowired
    private UserBankCardService userBankCardService;

    @ApiOperation(value = "我的默认")
    @ApiResponses(@ApiResponse(code = 10000, response = UserBankCard.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="cardType", required = true, value = "卡类型 alipay bank", paramType = "body"))
    @RequestMapping("/findDefault")
    public BaseResponse findDefault(@RequestBody @ApiIgnore Map<String, String> map) {
        String cardType = map.get("cardType");
        if(StringUtils.isBlank(cardType)) {
            return BaseResponse.fail("卡类型不能为空！");
        }
        Long userId = UserTokenUtil.getUser().getId();
        return userBankCardService.findDefault(userId,cardType);
    }

    @ApiOperation(value = "我的银行卡")
    @ApiResponses(@ApiResponse(code = 10000, response = UserBankCard.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="cardType", required = true, value = "卡类型 alipay bank", paramType = "body"))
    @RequestMapping("/page")
    public BaseResponse page(@RequestBody @ApiIgnore Map<String, String> map) {
        String cardType = map.get("cardType");
        if(StringUtils.isBlank(cardType)) {
            return BaseResponse.fail("卡类型不能为空！");
        }
        Long userId = UserTokenUtil.getUser().getId();
        return userBankCardService.page(userId,cardType);
    }

    @ApiOperation(value = "添加银行卡")
    @ApiResponses(@ApiResponse(code = 10000, response = UserBankCard.class, message = "content"))
    @RequestMapping("/add")
    public BaseResponse add(@RequestBody @Validated UserBankCardReq userBankCardReq) {
        Long userId = UserTokenUtil.getUser().getId();
        return userBankCardService.add(userId, userBankCardReq);
    }

    @ApiOperation(value = "获取银行卡")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "id", paramType = "body"))
    @RequestMapping("/findSingle")
    public BaseResponse findSingle(@RequestBody @ApiIgnore Map<String, String> map) {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }
        return userBankCardService.findSingle(id);
    }

    @ApiOperation(value = "修改银行卡")
    @ApiResponses(@ApiResponse(code = 10000, response = UserBankCard.class, message = "content"))
    @RequestMapping("/edit")
    public BaseResponse edit(@RequestBody @Validated UserBankCardReq userBankCardReq) {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }
        Long userId = UserTokenUtil.getUser().getId();
        return userBankCardService.edit(id,userId, userBankCardReq);
    }

    @ApiOperation(value = "删除银行卡")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "id", paramType = "body"))
    @RequestMapping("/del")
    public BaseResponse del() {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }
        Long userId = UserTokenUtil.getUser().getId();
        return userBankCardService.del(id,userId);
    }


}
